﻿@page "/register"

@using Microsoft.EntityFrameworkCore
@using BlazorAppAuth.Models
@inject IDbContextFactory<BlazorAppAuth.Data.BlazorAppAuthContext> DbFactory
@inject NavigationManager NavigationManager

<div class="container">
    <div class="row justify-content-center align-items-center min-vh-100">
        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-body">
                    <h2 class="card-title text-center mb-4">用户注册</h2>
                    <EditForm method="post" Model="UserAccount" OnValidSubmit="AddUserAccount" FormName="create" Enhance>
                        <DataAnnotationsValidator />
                        <ValidationSummary class="text-danger" />
                        <div class="mb-3">
                            <label for="name" class="form-label">用户名:</label>
                            <InputText id="name" @bind-Value="UserAccount.Name" class="form-control" />
                            <ValidationMessage For="() => UserAccount.Name" class="text-danger" />
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">密码:</label>
                            <InputText id="password" @bind-Value="UserAccount.Password" type="password" class="form-control" />
                            <ValidationMessage For="() => UserAccount.Password" class="text-danger" />
                        </div>
                        <div class="mb-3">
                            <label for="role" class="form-label">权限:</label>
                            <InputSelect id="role" @bind-Value="UserAccount.Role" class="form-control">
                                @foreach (var role in UserRoles)
                                {
                                    <option value="@role">@role</option>
                                }
                            </InputSelect>
                            <ValidationMessage For="() => UserAccount.Role" class="text-danger" />
                        </div>
                        <button type="submit" class="btn btn-primary">Create</button>
                        <a class="btn" href="/login">登录</a>

                    </EditForm>
                </div>
            </div>
        </div>
    </div>
</div>



@code {

    [SupplyParameterFromForm]
    public UserAccount UserAccount { get; set; } = new();

    private List<string> UserRoles = new List<string> { "超级管理员", "管理员", "维修工", "操作员" };

    // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
    public async Task AddUserAccount()
    {
        using var context = DbFactory.CreateDbContext();
        context.UserAccount.Add(UserAccount);
        await context.SaveChangesAsync();
        NavigationManager.NavigateTo("/login");
    }
}
